package game.Utils;

public class AnimatedDouble {
    double beginValue, endValue;
    long durationMillis;

    long lastBeginTime;

    public AnimatedDouble(double startValue, double endValue, double duration, boolean beginAtEnd){
        this.beginValue = startValue;
        this.endValue = endValue;

        this.durationMillis = (long)(duration*1000);
        if(beginAtEnd){
            lastBeginTime = System.currentTimeMillis() - durationMillis;
        }else{
            lastBeginTime = System.currentTimeMillis();
        }
    }

    public AnimatedDouble(double startValue, double endValue, double duration){
        this(startValue, endValue, duration, false);
    }

    public void reset(){
        this.lastBeginTime = System.currentTimeMillis();
    }

    public double getValue(){
        if(System.currentTimeMillis() < lastBeginTime){
            return this.beginValue;
        }else if(System.currentTimeMillis() < lastBeginTime + durationMillis){
            double progress = (double)(System.currentTimeMillis() - lastBeginTime) / durationMillis;

            return this.endValue*progress + this.beginValue*(1.0-progress);
        }else{
            return this.endValue;
        }
    }
}
